In these exercises we are going to see what the compiler thinks is a sensible C program and how it reacts when our program is not correct.
-
Start up MPLAB X and create a new project as described earlier in the course.
-
Add the following code to create an simple C program and compile it.
/* my first program */ void main ( void ) { }
-
Change the
main
function name tomane
and recompile the program. Note what the compiler does and try to understand why it has done this.
-
Put the name
mane
back tomain
and then create a secondmain
function which looks just like the first (you can use block copy for this). Recompile the program and note that the compiler will complain again.
-
Go back to a single copy of the main file and take the open bracket off the
( void )
parameter list in the main function header.
Answers
-
You get the error : Failure Couldn't find function/label by name:main. This is because the linker is looking for a main function to start when the program runs, and not finding anything.
-
You get the error : error: Function 'main' already has a body This is because the compiler can't make sense of there being two different things with the same name.
-
You get the series of errors: error: missing semicolon error: failure This is because when the compiler sees a single closing bracket, it gets very confused and doesn't know quite what to do.